package text
{
	import flash.events.EventDispatcher;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.AntiAliasType;
	
	import mx.controls.TextArea;
	import mx.core.IUITextField;
	import mx.utils.StringUtil;
	
	import text.UiTextObject;

	public class SnapsTextStyleManager extends EventDispatcher
	{
		private var _text:String;
		private var _textarea:UiTextAreaObject;
		private var _contents:UiTextObject;
		private var _prevTextFormat:TextFormat;
		private var _currentTextFormat:TextFormat;
		
		/** Set UiTextObject **/
		public function set contents($value:UiTextObject):void
		{
			_contents = $value;
			
			if(_contents)
			{
				_prevTextFormat = $value.textformat;
				text = $value.textInfo.text;
				htmltext = $value.textInfo.htmltext;
				textarea.validateNow();
				clearText();
				_currentTextFormat = _prevTextFormat;
				textarea.textfield.defaultTextFormat = _currentTextFormat;
			}
		}
		
		/** Get UiTextObject **/
		public function get contents():UiTextObject
		{
			return _contents;
		}
		
		/** Set HTML Text **/
		public function set htmltext($value:String):void
		{
			trace("$value =",$value);
			_textarea.htmlText = $value;
		}
		
		/** Get HTML Text **/
		public function get htmltext():String
		{
			trace(_textarea.htmlText);
			return StringUtil.trim(_textarea.htmlText);
		}
		
		/** Set Contents Text **/
		public function set text($value:String):void
		{
			_text = $value;
			if(_textarea)
			{
				if(_text != "" && _text)
					_textarea.text = _text;
				else
					_text = "";
			}
		}
		
		/** Get Contents Text **/
		public function get text():String
		{
			return textarea.textfield.text;
		}
		
		/** Set FontFamily **/
		public function setFont($value:String):void
		{
			trace("setFont("+$value+":String)");
			checkTextFormat();
			_currentTextFormat.font = $value;
			applyTextFormat();
		}
		
		/** Set FontSize **/
		public function setSize($value:Number):void
		{
			trace("setSize("+$value+":String)");
			checkTextFormat();
			_currentTextFormat.size = $value;
			applyTextFormat();
		}
		
		/** Set Text Align **/
		public function setTextAlign($value:String):void
		{
			trace("setTextAlign("+$value+":String)");
			checkTextFormat();
			_currentTextFormat.align = $value;
			applyTextFormat(true);
		}
		
		/** Set Text Color **/
		public function setColor($value:uint):void
		{
			trace("setColor("+$value+":String)");
			checkTextFormat();
			_currentTextFormat.color = $value;
			applyTextFormat();
		}
		
		/** Set Text Bold **/
		public function setTextBold($value:Boolean):void
		{
			trace("setTextBold("+$value+":String)");
			checkTextFormat();
			_currentTextFormat.bold = $value;
			textarea.textfield.thickness = 2;
			//textarea.setStyle("fontWeight", "bold");
			//_currentTextFormat['bold'] = 'bold';
			applyTextFormat();
		}
		
		/** Set Text Italic **/
		public function setTextItalic($value:String):void
		{
			trace("setTextItalic("+$value+":String)");
			checkTextFormat();
			_currentTextFormat.italic = $value;
			applyTextFormat();
		}
		
		/** Set Target TextArea **/
		public function set textarea($value:UiTextAreaObject):void
		{
			_textarea = $value;
		}
		
		/** Get Target TextArea **/
		public function get textarea():UiTextAreaObject
		{
			return _textarea;
		}
		
		/** Get TextFormat **/
		public function get currentTextFormat():TextFormat
		{
			return textarea.textfield.getTextFormat();
		}
		
		public function SnapsTextStyleManager($textarea:UiTextAreaObject)
		{
			textarea = $textarea;
			init();
		}
		
		
		
		private function init():void
		{
			if(!_prevTextFormat)
				_prevTextFormat = textarea.textfield.defaultTextFormat;
			
			textarea.textfield.defaultTextFormat = _prevTextFormat;
			textarea.textfield.alwaysShowSelection = true;
			textarea.textfield.multiline = true;
			//textarea.textfield.embedFonts = true;
			textarea.textfield.wordWrap = true;
			textarea.textfield.antiAliasType = AntiAliasType.ADVANCED;
			
		}
		
		/** Clear TextField Text **/
		public function clearText():void
		{
			var len:int = textarea.textfield.text.length;
			
			var obj:IUITextField = textarea.textfield;
			
			if(obj.text.lastIndexOf("\r") == len-1)
				obj.replaceText(obj.text.lastIndexOf("\r"), len ,"");
			
		}
		
		
		/** Check TextFormat **/
		private function checkTextFormat():void
		{
			/** Set All Texts Style **/
			if(textarea.textfield.selectionBeginIndex == textarea.textfield.selectionEndIndex)
			{
				if(!_prevTextFormat)
					_prevTextFormat = new TextFormat;
			}
			else
			{
				_prevTextFormat = new TextFormat;
			}
			
			_currentTextFormat = _prevTextFormat;
			trace("BOLD=",_currentTextFormat.bold);
		}
		
		/** Apply TextFormat **/
		private function applyTextFormat($all:Boolean = false):void
		{
			var begin:int = textarea.textfield.selectionBeginIndex;
			var end:int = textarea.textfield.selectionEndIndex;
			
			if($all == true)
				textarea.textfield.setTextFormat( _currentTextFormat );
			else if(begin != end)
				textarea.textfield.setTextFormat(_currentTextFormat, begin, end);
			else
				textarea.textfield.defaultTextFormat = _currentTextFormat;
			
			trace("BOLD=",_currentTextFormat.bold);
			
			//textarea.textfield.validateNow();
			
			//textarea.text = textarea.text;
			textarea.textfield.htmlText = textarea.textfield.htmlText;
			//textarea.text = textarea.text;
			trace(textarea.textfield.htmlText);
		}
	}
}